package com.zima.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zima.mall.entity.MallUser;
import com.zima.mall.mapper.LoginInformationMapper;
import com.zima.mall.service.LoginService;
import com.zima.mall.util.MD5Utils;
import com.zima.mall.vo.LoginVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginInformationMapper loginInformationMapper;

    @Override
    public int findByName(LoginVo loginVo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("account", loginVo.getAccount());

        MallUser mallUser = loginInformationMapper.selectOne(wrapper);

        if (mallUser != null) {
            //等待项目完结之后把括号里面的内容改为 "MD5Utils.inputPassToFormPass(loginVo.getPassword())"
            if (mallUser.getPassword().equals(MD5Utils.inputPassToFormPass(loginVo.getPassword()))) {//判断密码是否输入正确
                if (mallUser.getRole() == 1) {//如果role值为1则表示其为一般用户
                    return 1;
                } else if (mallUser.getRole() == 2) {//如果role值为2则表示其为管理员
                    return 2;
                }
            }
        }
        return 0;
    }
}

